/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * UploadPanel.java
 *
 * Created on Jul 5, 2009, 3:07:50 PM
 */
package br.unisc.pdaapp.ui;

import br.unisc.pdaapp.constants.IPanels;
import br.unisc.pdaapp.meta.FileUtil;
import br.unisc.pdaapp.webservice.client.WSHandler;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import javax.swing.JLabel;
import org.kobjects.base64.Base64;

/**
 *
 * @author guilherme
 */
public class UploadPanel extends javax.swing.JPanel implements IPanels
{

    private static UploadPanel instance = null;

    /** Creates new form UploadPanel */
    private UploadPanel()
    {
        initComponents();
    }

    public static UploadPanel getInstance()
    {
        if (instance == null)
        {
            instance = new UploadPanel();
        }

        return instance;
    }

    public void listFiles(final File currentDir)
    {
        //limpa o panel
        centerPanel.removeAll();

        File[] files = currentDir.listFiles();

        GridBagConstraints gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.anchor = GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new Insets(0, 2, 0, 2);
        gridBagConstraints.weightx = 1;
        gridBagConstraints.gridy = 0;
        if (files.length == 0)
        {
            gridBagConstraints.weighty = 1;
        }

        if (currentDir.getParentFile() != null)
        {
            JLabel labelFile = new JLabel(".. acima");
            labelFile.setFont(new java.awt.Font("Tahoma", 0, 10));
            labelFile.addMouseListener(new MouseAdapter()
            {

                public void mouseClicked(MouseEvent e)
                {
                    listFiles(currentDir.getParentFile());
                }
            });
            centerPanel.add(labelFile, gridBagConstraints);
        }

        for (int x = 0; x < files.length; x++)
        {
            gridBagConstraints.weighty = ((x == files.length - 1) ? 1 : 0);
            gridBagConstraints.gridy = x + 1;

            final File file = files[x];

            JLabel labelFile = new JLabel(file.getName());

            javax.swing.ImageIcon fileIcon = new javax.swing.ImageIcon(getClass().getResource("/br/unisc/pdaapp/ui/images/file.png"));
            javax.swing.ImageIcon directoryIcon = new javax.swing.ImageIcon(getClass().getResource("/br/unisc/pdaapp/ui/images/folder.jpeg"));
            labelFile.setIcon(file.isDirectory() ? directoryIcon : fileIcon);
            labelFile.setFont(new java.awt.Font("Tahoma", 0, 10));

            labelFile.addMouseListener(new MouseAdapter()
            {

                public void mouseClicked(MouseEvent e)
                {
                    if (file.isDirectory())
                    {
                        listFiles(file);
                    }
                    else
                    {
                        uploadFile(file);
                    }
                }
            });

            centerPanel.add(labelFile, gridBagConstraints);
        }

        centerPanel.revalidate();
        centerPanel.repaint();
    }

    private void uploadFile(File file)
    {
        try
        {
            WSHandler wsHandler = new WSHandler();
            int courseID = CourseClasseslPanel.getInstance().getCourseID();
            int courseSectionID = MaterialPanel.getInstance().getCourseSectionID();
            byte[] bytes = FileUtil.getBytesFromFile(file);

            String message = "";
            if (wsHandler.uploadMaterial(courseID, courseSectionID, Base64.encode(bytes), file.getName()) == -1)
            {
                message = "Erro no upload!";
            }
            else
            {
                message = "Upload ok";
            }

            MaterialPanel.getInstance().getMessageLabel().setText(message);
            MoodleMobileFrame.getInstance().switchPanel(IPanels.MATERIAL_PANE);
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        northPanel = new javax.swing.JPanel();
        welcomeLabel = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        backButton1 = new javax.swing.JButton();
        exitButton = new javax.swing.JButton();
        messageLabel = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        centerPanel = new javax.swing.JPanel();

        setBackground(new java.awt.Color(255, 255, 255));
        setLayout(new java.awt.BorderLayout());

        northPanel.setOpaque(false);
        northPanel.setLayout(new java.awt.GridBagLayout());

        welcomeLabel.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
        welcomeLabel.setText("Escolha um arquivo:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(4, 2, 4, 2);
        northPanel.add(welcomeLabel, gridBagConstraints);

        jPanel1.setOpaque(false);
        jPanel1.setLayout(new java.awt.GridBagLayout());

        backButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/unisc/pdaapp/ui/images/back.jpeg"))); // NOI18N
        backButton1.setBorder(null);
        backButton1.setOpaque(false);
        backButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                backButton1ActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 1);
        jPanel1.add(backButton1, gridBagConstraints);

        exitButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/unisc/pdaapp/ui/images/close.jpeg"))); // NOI18N
        exitButton.setBorder(null);
        exitButton.setOpaque(false);
        exitButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        jPanel1.add(exitButton, gridBagConstraints);

        messageLabel.setFont(new java.awt.Font("Tahoma", 0, 10));
        messageLabel.setForeground(new java.awt.Color(153, 153, 153));
        messageLabel.setText(" ");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(4, 2, 4, 2);
        jPanel1.add(messageLabel, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        northPanel.add(jPanel1, gridBagConstraints);

        add(northPanel, java.awt.BorderLayout.NORTH);

        centerPanel.setBackground(new java.awt.Color(255, 255, 255));
        centerPanel.setLayout(new java.awt.GridBagLayout());
        jScrollPane1.setViewportView(centerPanel);

        add(jScrollPane1, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

    private void backButton1ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_backButton1ActionPerformed
    {//GEN-HEADEREND:event_backButton1ActionPerformed
        messageLabel.setText(" ");
        MoodleMobileFrame.getInstance().switchPanel(IPanels.MATERIAL_PANE);
}//GEN-LAST:event_backButton1ActionPerformed

    private void exitButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_exitButtonActionPerformed
    {//GEN-HEADEREND:event_exitButtonActionPerformed
        messageLabel.setText(" ");
        MoodleMobileFrame.getInstance().quit(this);
}//GEN-LAST:event_exitButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton backButton1;
    private javax.swing.JPanel centerPanel;
    private javax.swing.JButton exitButton;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel messageLabel;
    private javax.swing.JPanel northPanel;
    private javax.swing.JLabel welcomeLabel;
    // End of variables declaration//GEN-END:variables

}
